<template>
  <view>
    <search-input class="search-view"></search-input>
    <view class="container">
      <menu-tool @change="menuChange"></menu-tool>
      <goods-item
        v-for="item in dataList"
        :key="item.goods_id"
        :item="item"
        @click.native="goodsAction(item)"
      ></goods-item>
    </view>
  </view>
</template>

<script>
export default {
  onLoad: function (option) {
    const param = JSON.parse(decodeURIComponent(option.param))
    if (param.cid) {
      this.param.cid = param.cid
    }
    if (param.query) {
      this.param.query = param.query
    }
    this.getGoodsSearch()
  },

  data() {
    return {
      param: {
        pagenum: 1,
        pagesize: 10,
      },

      dataList: [],
      isMoreData: true,
    }
  },

  onPullDownRefresh() {
    this.param.pagenum = 1
    this.getGoodsSearch()
  },

  onReachBottom() {
    if (this.isMoreData) {
      this.param.pagenum++
      this.getGoodsSearch(false)
    }
  },

  methods: {
    async getGoodsSearch(isPull = true) {
      const param = this.param
      const value = await this.$http.goodsSearch(param)
      if (isPull) {
        this.dataList = value.goods
      } else {
        this.dataList = [...this.dataList, ...value.goods]
      }
      this.isMoreData = this.dataList.length < value.total
      uni.stopPullDownRefresh()
    },

    menuChange(index) {
      this.dataList.sort((value1, value2) => {
        if (index == 0) {
          return value2.goods_id - value1.goods_id
        } else if (index == 1) {
          return value1.hot_mumber - value2.hot_mumber
        } else {
          return value1.goods_price - value2.goods_price
        }
      })
    },

    goodsAction(item) {
      this.$mrouter.push("/pages/detail/index", item.goods_id)
    },
  },
}
</script>

<style lang="scss" scoped>
.search-view {
  position: fixed;
  z-index: 1;
  left: 0;
  right: 0;
  top: var(--window-top);
}

.container {
  margin-top: 100rpx;
}
</style>
